﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;


namespace scomA2Proj
{
    
   

    public class Retailer
    {
        public int CCNum { get; set; }
        private MulticellBuffer mcBuff;
        private decimal lastPrice = -1;
        private int threadID = -1;
        bool shouldStop = false;
        Stopwatch stopwatchR = new Stopwatch();
        /// <summary>
        /// Creates a Retailer.
        /// </summary>
        /// <param name="buf">The multicell buffer</param>
        /// <param name="ccNum">Credit Card number.</param>
        public Retailer(MulticellBuffer buf, int ccNum)
        {
            this.mcBuff = buf;
            this.CCNum = ccNum;
        }


        /// <summary>
        /// Keep the retailer thread going.
        /// </summary>
        public void retailerStuff()
        {
            this.threadID = Thread.CurrentThread.ManagedThreadId;
            while (!shouldStop)
            {
                Thread.Sleep(2);
            }
        }

        /// <summary>
        /// Stop the retailer thread.
        /// </summary>
        public void stop()
        {
            shouldStop = true;
        }


        /// <summary>
        /// Handles the order complete.
        /// 1. Checks if this is the original Retailer
        /// 2. Calculates and prints the Order completion time and prints the reciept.
        /// </summary>
        /// <param name="reciept">The Order receipt from the Chicken farm.</param>
        public void OrderCompleteEventHandler(OrderReciept reciept)
        {
            if (reciept.OrdererID == threadID)
            {                
                reciept.stopwatch.Stop();
                stopwatchR.Stop();


                ConsoleUtil.AppendLine("======== Order Complete: ThreadID= " 
                    + Thread.CurrentThread.ManagedThreadId + " | Completion Time =" + stopwatchR.Elapsed + " =========\n" 
                    + reciept.ToString());
            }
        }

        /// <summary>
        /// Callback for price cut event sent from chicken farm.
        /// </summary>
        /// <param name="newPrice"></param>
        public void PriceCutEventHandler(decimal newPrice)
        {
            Order o;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            stopwatchR.Restart();
            
            //First order, just get 20 chickens
            if (lastPrice == -1)
            {
                o = new Order(stopwatch, threadID, CCNum, 20);
            }
            else //subsequent orders, increase based on amount of price cut.
            {
                decimal deltaPrice = newPrice - lastPrice;

                int numChickens = (int)(20.0m - deltaPrice * 1.5m);
                if (numChickens <= 0)
                    numChickens = 5;
                o = new Order(stopwatch, threadID, CCNum, numChickens);

            }
            mcBuff.putItem(Order.Encode(o));
            lastPrice = newPrice;
        }
    }
}
